package com.qinhao.loveCrowdFunding.service;

import javax.swing.table.DefaultTableCellRenderer;

import com.qinhao.loveCrowdFunding.enums.LoveCrowdFundingStatusEnum;
import com.qinhao.loveCrowdFunding.mapper.SimpleInformationMapper;
import com.qinhao.loveCrowdFunding.pojo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.qinhao.loveCrowdFunding.mapper.DetailInformationMapper;
import com.qinhao.loveCrowdFunding.req.DetailInformationRequest;

@Service
public class DetailInformationService {
    @Autowired
    private DetailInformationMapper detailInformationMapper;
    @Autowired
    private SimpleInformationMapper simpleInformationMapper;
    @Transactional
    public DetailInformationDTO writeDetailInformation(DetailInformationRequest detailInformationRequest) throws Exception {
        WriteSimpleInformationEntity writeSimpleInformationEntity = WriteSimpleInformationEntity.builder().build();
        WriteDetailInformationEntity writeDetailInformationEntity = WriteDetailInformationEntity.builder().build();
        WriteTelephoneEntity writeTelephoneEntity = WriteTelephoneEntity.builder().build();
        DetailInformationDTO detailInformationDTO = DetailInformationDTO.builder().build();
        if (detailInformationRequest != null) {
            BeanUtils.copyProperties(detailInformationRequest, writeSimpleInformationEntity);
            BeanUtils.copyProperties(detailInformationRequest, writeDetailInformationEntity);
        }
        //写入simpleInformation
        detailInformationMapper.writeSimpleInformation(writeSimpleInformationEntity);
        writeDetailInformationEntity.setSimpleInformationId(writeSimpleInformationEntity.getId());
        //写入detailInformation
        detailInformationMapper.writeDetailInformation(writeDetailInformationEntity);
        //如果没有写如果telephoneNumber，则写入telephoneNumber
        if (detailInformationRequest.getTelephoneNumber() != null && !detailInformationRequest.getTelephoneNumber().equals("")) {
            writeTelephoneEntity = WriteTelephoneEntity.builder().telephoneNumber(detailInformationRequest.getTelephoneNumber())
                    .userId(detailInformationRequest.getUserId()).build();
            detailInformationMapper.writeTelephoneNumber2User(writeTelephoneEntity);
        }
        detailInformationDTO.setId(writeDetailInformationEntity.getId());
        return detailInformationDTO;
    }

    public UpdateAllowDTO isAllowUpdate(Integer id) {
        UpdateAllowDTO updateAllowDTO = UpdateAllowDTO.builder().build();
        SimpleInformationStatusBO simpleInformationStatus = simpleInformationMapper.getSimpleInformationStatus(id);
        System.out.println(simpleInformationStatus.getStatus());
        if (simpleInformationStatus.getStatus().intValue() == LoveCrowdFundingStatusEnum.NOTAPPROVED.getCode().intValue() || simpleInformationStatus.getStatus().intValue() == LoveCrowdFundingStatusEnum.FAILEDPASS.getCode().intValue() ) {
            updateAllowDTO.setAllowUpdate(true);
        } else {
            updateAllowDTO.setAllowUpdate(false);
        }
        return updateAllowDTO;
    }
}
